import { Provide, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { DPModelInfoCreateDto, DPModelInfoUpdateDto, DPModelInfoQueryDto, DPModelInfoDeleteDto } from '../dto/ModelInfo.dto'
import { TB_DPModel_Info } from '../entity/ModelInfo.entity';

@Provide()
export class DPModelInfoService {
  @InjectEntityModel(TB_DPModel_Info)
  dpModelInfoRepository: Repository<TB_DPModel_Info>;

  // 创建模型信息
  async createModelInfo(params: DPModelInfoCreateDto) {
    try {
      const modelInfo = new TB_DPModel_Info();
      modelInfo.MDName = params.MDName;
      modelInfo.EngineID = params.EngineID;
      modelInfo.EngineType = params.EngineType;
      modelInfo.SampleCount = params.SampleCount;
      modelInfo.MDFileName = params.MDFileName;
      modelInfo.MDFilePath = params.MDFilePath;
      modelInfo.Mark = params.Mark;

      const result = await this.dpModelInfoRepository.save(modelInfo);

      return {
        success: true,
        data: result,
        message: '创建模型信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: `创建模型信息失败: ${error.message}`,
      };
    }
  }

  // 更新模型信息
  async updateModelInfo(params: DPModelInfoUpdateDto) {
    const { OBJID } = params;
    try {
      const modelInfo = await this.dpModelInfoRepository.findOne({ where: { OBJID } });

      if (!modelInfo) {
        return {
          success: false,
          message: '要更新的模型信息不存在',
        };
      }

      modelInfo.MDName = params.MDName ?? modelInfo.MDName;
      modelInfo.EngineID = params.EngineID ?? modelInfo.EngineID;
      modelInfo.EngineType = params.EngineType ?? modelInfo.EngineType;
      modelInfo.SampleCount = params.SampleCount ?? modelInfo.SampleCount;
      modelInfo.MDFileName = params.MDFileName ?? modelInfo.MDFileName;
      modelInfo.MDFilePath = params.MDFilePath ?? modelInfo.MDFilePath;
      modelInfo.Mark = params.Mark ?? modelInfo.Mark;

      const result = await this.dpModelInfoRepository.save(modelInfo);

      return {
        success: true,
        data: result,
        message: '更新模型信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: `更新模型信息失败: ${error.message}`,
      };
    }
  }

  // 查询模型信息（分页）
  async findModelInfo(query: DPModelInfoQueryDto) {
    const { MDName, EngineID, EngineType, page = 1, limit = 10 } = query;
    const where: any = {};

    // 根据模型名称查询
    if (MDName) {
      where['MDName'] = MDName;
    }

    // 根据引擎ID查询
    if (EngineID) {
      where['EngineID'] = EngineID;
    }

    // 根据引擎类型查询
    if (EngineType) {
      where['EngineType'] = EngineType;
    }

    try {
      const [modelInfos, total] = await this.dpModelInfoRepository.findAndCount({
        where,
        skip: (page - 1) * limit, // 分页起始位置
        take: limit,              // 每页返回数量
      });

      return {
        success: true,
        data: modelInfos,
        total,                     // 返回数据总数
        message: '查询模型信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: `查询模型信息失败: ${error.message}`,
      };
    }
  }

  // 删除模型信息
  async deleteModelInfo(params: DPModelInfoDeleteDto) {
    const { OBJID } = params;
    try {
      const modelInfo = await this.dpModelInfoRepository.findOne({ where: { OBJID } });

      if (!modelInfo) {
        return {
          success: false,
          message: '要删除的模型信息不存在',
        };
      }

      await this.dpModelInfoRepository.remove(modelInfo);

      return {
        success: true,
        message: '删除模型信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: `删除模型信息失败: ${error.message}`,
      };
    }
  }
}
